<?php
class Validator{
	
	/**
	 * List the available validators.
	 *
	 * @return array
	 */
	public static function validators(){
		cmspad_import('cache.DirectoryCache');
		$cache = new DirectoryCache('cmspad_validators',dirname(__FILE__) . '/validator/',DirectoryCache::$MODE_FILE,false);
		$files = array();
		foreach($cache->value() as $f => $p){
			$l = strlen($f) > 5?strlen($f) - 4:0;
			if($l > 0 && substr($f,$l) == '.php'){
				$files[substr($f,0,$l)] = $p;
			}
		}
		return $files;
	}
	
	public static function getValidatorScripts(){
		
		$tmpfile = CMSPAD_TEMPORARY_ABSPATH . 'validators.js';
		
		if(file_exists($tmpfile) && !CMSPAD_DEBUG){
			return "\t<script type=\"text/javascript\" src=\"" . CMSPAD_TEMPORARY_RELPATH . "validators.js\"></script>\r\n";
		}else{
			$validators = self::validators();
			$javascripts = array();
			foreach($validators as $k=>$v){
				include ($v);
				$className = cmspad_get_class_name('cmspad_validator_' . $k);
				if($className){
					$methodName = cmspad_get_function_name('getJavascript','cmspad_validator_' . $k);
					if($methodName){
						$javascript = call_user_func(array($className,$methodName),'v');
						if($javascript){
							$javascripts[$k] = $javascript;
						}
					}
				}
			}
			
			if($javascripts){
				$validatorScripts = 'cmspad.apply(cmspad.validator, {';
				foreach($javascripts as $f=>$c){
					$validatorScripts .= '_' . $f . ':function(v){' . $c . '},';
				}
				$validatorScripts = trim($validatorScripts,',') . '});';
				
				if(!CMSPAD_DEBUG){
					$class = cmspad_import('util.JavaScriptPacker');
					if($class){
						$packer = new $class($validatorScripts);
						$packedScripts = $packer->pack();
					}else{
						$packedScripts = $validatorScripts;
					}
				}else{
					$packedScripts = $validatorScripts;
				}
			}else{
				$packedScripts = null;
			}
			
			if($packedScripts){
				if(is_writable(CMSPAD_TEMPORARY_ABSPATH) && !CMSPAD_DEBUG){
					$fp = fopen($tmpfile,'w');
					fwrite($fp,$packedScripts);
					fflush($fp);
					fclose($fp);
					@chmod($tmpfile,0777);
					@touch($tmpfile,filemtime(dirname(__FILE__) . '/validators'));
					return "\t<script type=\"text/javascript\" src=\"" . CMSPAD_TEMPORARY_RELPATH . "validators.js\"></script>\r\n";
				}else{
					return "\t<script type=\"text/javascript\">" . $packedScripts . "</script>\r\n";
				}
			}else{
				return null;
			}
		}
	}
	
	public static function validate($type,$value,$context = null){
		$type = strtolower($type);
		$validatorFile = dirname(__FILE__) . '/validator/' . $type . '.php';
		if(!file_exists($validatorFile)){
			return null;
		}
		
		include ($validatorFile);
		$className = cmspad_get_class_name('cmspad_validator_' . $type);
		if($className){
			$methodName = cmspad_get_function_name('validate','cmspad_validator_' . $type);
			if($methodName){
				$validatorObj = new $className();
				$result = $validatorObj->$methodName($value,$context);
				unset($validatorObj);
				return $result;
			}
		}
		return null;
	}
	
	public function __call($name,$args){
		if($args && count($args) > 0){
			if(count($args) > 1){
				return self::validate($name,$args[0],$args[1]);
			}else{
				return self::validate($name,$args[0]);
			}
		}
		return null;
	}
}
?>
